﻿<cfsilent>
	<cfscript>
	
		jsonAdvice = getProperty("serviceFactory").getBean("jsonAdvice");

		response = structNew();
		structInsert(response, "success", false, true);

		taskId = event.getArg("TSK");
		taskType = event.getArg("TT");
		
		step = event.getArg("STP");

		transaction {
			
			/* 载入教学任务 */
			switch(taskType){

				case "P":
					sql = "	SELECT
								t.tsk_id,t.task_capacity,t.task_limite,t.task_chooesd
							FROM
								t_task t
							WHERE
								t.tsk_id = :taskId ";
					break;

				case "C":
					sql = "	SELECT
								t.tsk_id,t.task_capacity,t.task_limite,t.task_chooesd
							FROM
								t_common_task t
							WHERE
								t.tsk_id = :taskId ";
					break;
					
				case "R":
					sql = "	SELECT
								t.tsk_id,t.task_capacity,t.task_limite,t.task_chooesd
							FROM
								t_reopen_task t
							WHERE
								t.tsk_id = :taskId ";
					break;
					
				case "T":
					sql = "	SELECT
								t.tsk_id,t.task_capacity,t.task_limite,t.task_chooesd
							FROM
								t_sport_task t
							WHERE
								t.tsk_id = :taskId ";
					break;
					
			}
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
			rs_taskEntity = queryObj.execute(sql = sql).getResult();
			
			if ( step eq "maxsize" ) {
				
				/*直接扩容为最大容量*/
				if ( rs_taskEntity.task_capacity gt 0 ) {
					
					newSize = rs_taskEntity.task_capacity;
					
					switch(taskType){
						case "P":
							sql = "	UPDATE
										t_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
						case "C":
							sql = "	UPDATE
										t_common_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
						case "R":
							sql = "	UPDATE
										t_reopen_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
						case "T":
							sql = "	UPDATE
										t_sport_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
					}
					
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
					queryObj.addParam(name = "newSize", value = newSize, cfsqltype = "cf_sql_integer");
					queryObj.execute(sql = sql);
					
					structInsert(response, "success", true, true);
					
				}
				else {
					structInsert(response, "reason", "无法读取教室容量", true);
				}
				
			} else if ( isNumeric(step) ) {
				
				if ( step gt 0 ) {
					
					if ( rs_taskEntity.task_capacity gt 0 ) {

						/* 增加计划人数 但在教室容量范围内 */
						newSize = min( rs_taskEntity.task_capacity, rs_taskEntity.task_limite + step );
					} else {
						newSize = rs_taskEntity.task_limite + step;
					}
					
					switch(taskType){
						case "P":
							sql = "	UPDATE
										t_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
						case "C":
							sql = "	UPDATE
										t_common_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
						case "R":
							sql = "	UPDATE
										t_reopen_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
						case "T":
							sql = "	UPDATE
										t_sport_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
					}
					
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
					queryObj.addParam(name = "newSize", value = newSize, cfsqltype = "cf_sql_integer");
					queryObj.execute(sql = sql);
					
					
					structInsert(response, "success", true, true);
				}

				if ( step lt 0 ) {
					
					/* 减少计划人数 但大于已选人数 */
					newSize = max( rs_taskEntity.task_chooesd, rs_taskEntity.task_limite + step );

					switch(taskType){
						case "P":
							sql = "	UPDATE
										t_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
						case "C":
							sql = "	UPDATE
										t_common_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
						case "R":
							sql = "	UPDATE
										t_reopen_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
						case "T":
							sql = "	UPDATE
										t_sport_task t
									SET
										t.task_limite = :newSize 
									WHERE
										t.tsk_id = :taskId ";
							break;
					}
					
					queryObj = new Query(datasource = application.dnsMaster);
					queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
					queryObj.addParam(name = "newSize", value = newSize, cfsqltype = "cf_sql_integer");
					queryObj.execute(sql = sql);
					
					structInsert(response, "success", true, true);
				}
				
			} else {
				structInsert(response, "reason", "错误的参数", true);
			}
			
			
			switch(taskType){

				case "P":
					sql = "	SELECT
								t.tsk_id,t.task_capacity,t.task_limite,t.task_chooesd
							FROM
								t_task t
							WHERE
								t.tsk_id = :taskId ";
					break;

				case "C":
					sql = "	SELECT
								t.tsk_id,t.task_capacity,t.task_limite,t.task_chooesd
							FROM
								t_common_task t
							WHERE
								t.tsk_id = :taskId ";
					break;
					
				case "R":
					sql = "	SELECT
								t.tsk_id,t.task_capacity,t.task_limite,t.task_chooesd
							FROM
								t_reopen_task t
							WHERE
								t.tsk_id = :taskId ";
					break;

				case "T":
					sql = "	SELECT
								t.tsk_id,t.task_capacity,t.task_limite,t.task_chooesd
							FROM
								t_sport_task t
							WHERE
								t.tsk_id = :taskId ";
					break;
					
			}
			
			queryObj = new Query(datasource = application.dnsMaster);
			queryObj.addParam(name = "taskId", value = taskId, cfsqltype = "cf_sql_varchar");
			rs_taskEntity = queryObj.execute(sql = sql).getResult();
			
			structInsert(response, "planSize", rs_taskEntity.task_limite, true);
		}
	
	
	</cfscript>
</cfsilent><cfoutput>#jsonAdvice.encode(data:response, stringNumbers:true)#</cfoutput>